package com.allcontroller;

import com.wzy.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@RestController
@CrossOrigin
public class Controller5 {
    @PostMapping("/fileUpload")
    public Result fileUpload(MultipartFile[] files , HttpServletRequest request){
        for (MultipartFile file : files){
            String fileName = file.getOriginalFilename();
            String subfix = fileName.substring(fileName.lastIndexOf("."));
            if(!".jpg".equalsIgnoreCase(subfix)){
                return  new Result(false , "上传失败，只能上传jpg格式的图片");
            }
            fileName = UUID.randomUUID() + subfix;
            String dirPath = "src/main/resources/backend/upload";
            File filePath = new File(dirPath);
            if (!filePath.exists()){
                filePath.mkdirs();
            }
            try{
                file.transferTo(new File("D:\\code\\java\\ideaworkspace\\springboot-experiment-all\\Springboot-02\\src\\main\\resources\\backend\\upload"+"\\"+fileName));
                return new Result(false , "上传成功");
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,"上传失败！");
            }
        }
        return  new Result(false,"上传失败！");
    }
}
